package com.zjw.core.algorithm;

/**
 * 求两个大数之和
 * */
public class BigNumberSum {

    public static void main(String[] args) {
        String x = "1002";
        String y = "834783748";
        char[] large = null;
        char[] small = null;
        if(x.length() > y.length()){
            large = x.toCharArray();
            small = y.toCharArray();
        }else {
            large = y.toCharArray();
            small = x.toCharArray();
        }
        int[] sum = new int[large.length + 1];
        for (int i = 0; i < large.length; i++) {
            sum[i] = large[large.length -i - 1] - '0';//char转int使用 -'0'操作
        }
        for (int i = 0; i < small.length; i++) {
            sum[i] = sum[i] + small[small.length -i - 1] - '0';
        }
        StringBuilder stringBuilder = new StringBuilder();
        for (int i = 0; i < sum.length; i++) {
            if(sum[i] >= 10){
                sum[i+1] = sum[i+1] + 1;
                sum[i] = sum[i] % 10;
            }
            stringBuilder.append(sum[i]);
        }
        String result = stringBuilder.reverse().toString();
        if(stringBuilder.toString().startsWith("0")){
            result = stringBuilder.substring(1);
        }
        System.out.println(result);
    }
}
